package app {
	import app.ui.panels.OptionsPanel;
	import app.ui.panels.BMELoadPanel;
	import app.ui.panels.EventBrowserPanel;
	import app.ui.panels.IBrowserPanel;

	import bme.data.BMELoader;
	import bme.data.datatypes.BRC_Event;

	import com.greensock.TweenLite;
	import com.greensock.easing.Strong;

	import flash.display.Sprite;

	/**
	 * @author Rendall
	 */
	public class Main extends Sprite {

		private var bmeLoadPanel : BMELoadPanel;
		private var bmeLoader : BMELoader;
		private var eventBrowser : EventBrowserPanel;
		private var currPanel : IBrowserPanel;
		private var optionsPanel : OptionsPanel;
		
		public static const SORT_OPTIONS:Array = [{label:"Time", option:"time"}, {label:"Camp", option:"camp"}, {label:"Event ID", option:"id"}, {label:"Shuffle!", option:"shuffle"}];

		public function Main() {
			
			TweenLite.defaultEase = Strong.easeInOut;

			bmeLoader = BMELoader.getInstance();
			bmeLoader.statusSignal.add(onBMELoadStatusChange);
						
			bmeLoadPanel = new BMELoadPanel(this);
			eventBrowser = new EventBrowserPanel(this);
			eventBrowser.stateChangeSignal.add(onPanelStateChange);
			eventBrowser.hide();
			
			optionsPanel = new OptionsPanel(this);
			optionsPanel.stateChangeSignal.add(onPanelStateChange);
			optionsPanel.hide();
			
			show(bmeLoadPanel);
		}

		private function onPanelStateChange(state : String, value:*=null) : void {
			
			switch (state) {
				case EventBrowserPanel.STATE_OPTION_PUSH:
					optionsPanel.show();
					break;                    
				case OptionsPanel.STATE_CLOSE_PRESS:
					optionsPanel.hide();
					break;
				case OptionsPanel.STATE_SORT_SELECT:
					eventBrowser.sortBy(value);
					break;
				default:
					trace("Unknown state '"+state+ "' with value '" + value.toString() + "'");
			}
		}

		private function show(panel : IBrowserPanel) : void {
			panel.show();
			if (currPanel) currPanel.hide();
			currPanel = panel;
		}

		private function onBMELoadStatusChange(status : String, value : *) : void {

			switch (status) {
				case BMELoader.STATUS_NEW_ITEM: 
					//TODO: This is assumed to be events, but will eventually be more than that.
					break;                    
				case BMELoader.STATUS_PARSE_COMPLETE:
					eventBrowser.setEventsArray(BRC_Event.events);
					show(eventBrowser);
					break;
			}
		
			trace("\n BMELoadPanel.onStatusChange( status:" + status + ")");
		}
	}
}
